#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "globalvars.h"
#include <QDebug>
#include <QStringBuilder>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_isCloseSystem = false;
    m_msgSocket = new MsgSocket;

    m_loginForm = new LoginForm(ui->widget);
    m_envForm = new RealTimeForm(ui->widget);
    m_devForm = new HomeDevForm(ui->widget);
    m_dataAnalyForm = new DataAnalystForm(ui->widget);
    m_chatForm = new HomeChatForm(ui->widget);
    m_personForm = new PersonInfoForm(ui->widget);

    connect(m_loginForm, SIGNAL(signalUserLogin(QString,QString)),
            this, SLOT(slotUserLogin(QString,QString)));
    connect(m_msgSocket, SIGNAL(signalUserLoginResult(bool)),
            this, SLOT(slotUserLoginResult(bool)));
    connect(m_msgSocket, SIGNAL(signalUserLogoutResult(bool)),
            this, SLOT(slotUserLogoutResult(bool)));
    connect(m_chatForm, SIGNAL(signalHomeChat(QString)),
            this, SLOT(slotHomeChat(QString)));
    connect(m_msgSocket, SIGNAL(signalAppengChatMsg(QString,QString,QString)),
            m_chatForm, SLOT(slotAppengChatMsg(QString,QString,QString)));

    connect(m_envForm, SIGNAL(signalGainTemp(QString)),
            m_msgSocket, SLOT(slotSendMsg(QString)));
    connect(m_envForm, SIGNAL(signalGainHumi(QString)),
            m_msgSocket, SLOT(slotSendMsg(QString)));
    connect(m_envForm, SIGNAL(signalGainTips(QString)),
            m_msgSocket, SLOT(slotSendMsg(QString)));
    connect(m_msgSocket, SIGNAL(signalparseDevTemp(QString)),
            m_envForm, SLOT(slotparseDevTemp(QString)));
    connect(m_msgSocket, SIGNAL(signalparseDevHumi(QString)),
            m_envForm, SLOT(slotparseDevHumi(QString)));
    connect(m_msgSocket, SIGNAL(signalparseDevMode(QString)),
            m_envForm, SLOT(slotparseDevMode(QString)));

    connect(m_devForm, SIGNAL(signalSendMsg(QString)),
            m_msgSocket, SLOT(slotSendMsg(QString)));
    connect(m_msgSocket, SIGNAL(signalparseDevStat(QString)),
            m_devForm, SLOT(slotParseDevStat(QString)));

    m_loginForm->show();
    m_envForm->hide();
    m_devForm->hide();
    m_dataAnalyForm->hide();
    m_chatForm->hide();
    m_personForm->hide();

    ui->mainToolBar->hide();
    ui->menuBar->hide();
    ui->statusBar->hide();
    this->setContextMenuPolicy(Qt::NoContextMenu); //去掉工具栏的右键菜单
    this->setCentralWidget(ui->widget);

    m_msgSocket->start();
}

MainWindow::~MainWindow()
{
    m_msgSocket->exitThread();
    if(m_msgSocket->wait())
    {
        delete m_msgSocket;
    }
    delete ui;
}

void MainWindow::slotUserLogin(QString id, QString pswd)
{
    ///用户登录请求
    QString msg = QString(CMD_UserLogin_L)
            % QString("#") % id
            % QString("|") % pswd;
    m_msgSocket->slotSendMsg(msg);
}

void MainWindow::slotUserLoginResult(bool res)
{
    if(res)
    {
        ui->mainToolBar->show();
        m_loginForm->hide();
        m_personForm->updateInfo();
        on_actionEnv_triggered();
    }else
    {
        m_loginForm->userLoginFail();
    }
}

void MainWindow::slotUserLogout(void)
{
    ///用户退出请求
    QString msg = QString(CMD_UserExit_X)
            % QString("#") % GlobalVars::g_localUsrInfo.getID();
    m_msgSocket->slotSendMsg(msg);
}

void MainWindow::slotUserLogoutResult(bool res)
{
    m_isCloseSystem = res;
    this->close();
}

void MainWindow::slotHomeChat(QString msg)
{
    ///家庭聊天
    QString info = QString(CMD_HomeChat_C)
            % QString("#") % GlobalVars::g_localUsrInfo.getName()
            % QString("|") % GlobalVars::g_localUsrInfo.getRole()
            % QString("|") % msg;
    m_msgSocket->slotSendMsg(info);
}

void MainWindow::on_actionEnv_triggered()
{
    ui->actionEnv->setEnabled(false);
    ui->actionDev->setEnabled(true);
    ui->actionData->setEnabled(true);
    ui->actionChat->setEnabled(true);
    ui->actionInfo->setEnabled(true);
    m_envForm->show();
    m_devForm->hide();
    m_dataAnalyForm->hide();
    m_chatForm->hide();
    m_personForm->hide();
}

void MainWindow::on_actionDev_triggered()
{
    ui->actionEnv->setEnabled(true);
    ui->actionDev->setEnabled(false);
    ui->actionData->setEnabled(true);
    ui->actionChat->setEnabled(true);
    ui->actionInfo->setEnabled(true);
    m_envForm->hide();
    m_devForm->show();
    m_dataAnalyForm->hide();
    m_chatForm->hide();
    m_personForm->hide();
}

void MainWindow::on_actionData_triggered()
{
    ui->actionEnv->setEnabled(true);
    ui->actionDev->setEnabled(true);
    ui->actionData->setEnabled(false);
    ui->actionChat->setEnabled(true);
    ui->actionInfo->setEnabled(true);
    m_envForm->hide();
    m_devForm->hide();
    m_dataAnalyForm->show();
    m_chatForm->hide();
    m_personForm->hide();
}

void MainWindow::on_actionChat_triggered()
{
    ui->actionEnv->setEnabled(true);
    ui->actionDev->setEnabled(true);
    ui->actionData->setEnabled(true);
    ui->actionChat->setEnabled(false);
    ui->actionInfo->setEnabled(true);
    m_envForm->hide();
    m_devForm->hide();
    m_dataAnalyForm->hide();
    m_chatForm->show();
    m_personForm->hide();
}

void MainWindow::on_actionInfo_triggered()
{
    ui->actionEnv->setEnabled(true);
    ui->actionDev->setEnabled(true);
    ui->actionData->setEnabled(true);
    ui->actionChat->setEnabled(true);
    ui->actionInfo->setEnabled(false);
    m_envForm->hide();
    m_devForm->hide();
    m_dataAnalyForm->hide();
    m_chatForm->hide();
    m_personForm->show();
}
